﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class EmployeesSalary_EmpSalarySummaryAllList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SelUser.SelUserInti("", "");
            SelUser.IsAllEmp = true;
            start.Text = DateTime.Now.Year.ToString() + "-1";
            end.Text = DateTime.Now.ToString("yyyy-M");

            MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
            master.HelpHref = "";
            master.PageName = "薪资汇总(全部)";
            master.PicUrl = "";
            master.IntiBar();
        }
    }

    private void wgvBind(DataTable dt)
    {


        if (wgvMaster.Columns.Count > 0)
        {
            wgvMaster.Columns.Clear();
        }
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            string[] FieldId = dt.Columns[i].ToString().Split('|');

            BoundField field = new BoundField();
            field.DataField = dt.Columns[i].ToString();
            field.HeaderText = FieldId[1].ToString();
            field.DataFormatString = "{0:##0.00}";

            wgvMaster.Columns.Add(field);
        }
        wgvMaster.ShowFooter = true;
        wgvMaster.DataSource = dt;
        wgvMaster.DataBind();

    }
    protected void Search_Click(object sender, EventArgs e)
    {
        string mode = ddlSelType.SelectedValue;
        if (Depart.SelectDepmanetId == "0" && mode == "name" && SelUser.EmployeeId == "")
        {
            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('按人名汇总，未选择人员\\r防止产生大量数据，请选择某一部门')", true);
            return;
        }


        string st = start.Text;
        string et = end.Text;
        string id = SelUser.EmployeeId;
        string dpt = Depart.SelectDepmanetId;
        string framework = wddlFramework.SelectedValue == "-1" ? "" : wddlFramework.SelectedValue;
        object[] ret = Srvtools.CliUtils.CallMethod("SEmployees", "EmpSalarySummaryAllList", new object[] { mode, st, et, id, dpt, framework });
        wgvBind(((DataSet)ret[1]).Tables[0]);
    }

    protected void wddlFramework_DataBound(object sender, EventArgs e)
    {
        wddlFramework.Items.Remove(new ListItem("——全部——", "-1"));

        wddlFramework.Items.Add(new ListItem("——全部——", "-1"));

        wddlFramework.SelectedValue = "-1";
    }
    protected void remove_Click(object sender, EventArgs e)
    {
        SelUser.EmpName = "";

        SelUser.EmployeeId = "";
    }

    protected void wgvMaster_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        wgvMaster.PageIndex = e.NewPageIndex;
        string mode = ddlSelType.SelectedValue;
        string st = start.Text;
        string et = end.Text;
        string id = SelUser.EmployeeId;
        string dpt = Depart.SelectDepmanetId;
        string framework = wddlFramework.SelectedValue == "-1" ? "" : wddlFramework.SelectedValue;
        object[] ret = Srvtools.CliUtils.CallMethod("SEmployees", "EmpSalarySummaryAllList", new object[] { mode, st, et, id, dpt, framework });
        wgvBind(((DataSet)ret[1]).Tables[0]);
    }
    protected void wgvMaster_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            DataTable dts = ((DataTable)wgvMaster.DataSource);
            int start = ddlSelType.SelectedValue == "time" ? 2 : 1;
            for (int i = start; i <= e.Row.Cells.Count; i++)
            {
                if (i > start)
                {
                    e.Row.Cells[i - 1].Text = dts.Compute(string.Format("sum([{0}])", dts.Columns[i - 1].ColumnName), "").ToString();
                }
                else
                {
                    e.Row.Cells[0].Text = "合计：";
                }
            }
        }
    }

    protected void DownLoad_Click(object sender, EventArgs e)
    {
        try
        {

            Search_Click(null, null);
            object obj = wgvMaster.DataSource;

            string path = MapPath("~/UploadFile/Template/");

            DataTable dts = ((DataTable)obj);

            if (dts.Rows.Count <= 0)
            {
                ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('没有记录')", true);
                return;
            }

            DataTable dtt = dts.Clone();
            dtt.Merge(dts);
            Srvtools.InfoDataSet ids = new Srvtools.InfoDataSet();
            ids.RemoteName = "GLModule.cmdColDEF";
            ids.RealDataSet.Tables.Add(dtt);

            ids.ToExcel(0, path + "empsalarysum.xls");


            System.IO.FileInfo fn = new System.IO.FileInfo(path + "empsalarysum.xls");

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fn.Name);
            HttpContext.Current.Response.WriteFile(fn.FullName);
            HttpContext.Current.Response.End();
        }
        catch
        {

        }
    }

}
